package com.taxi.driver.module.recruit;

/**
 * Created by Curry on 2018/5/9.
 */

import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * 自定义EditText
 */
public class MyEditText extends AppCompatEditText
{

    private Context mContext;
//    private Drawable delete;

    public MyEditText(Context context) {
        super(context);
        this.mContext = context;

        init();
    }

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;

        init();
    }

    public MyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;

        init();
    }


    private void init() {
        setSingleLine();

//        delete = mContext.getResources().getDrawable(R.drawable.selector_delete);

        addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
//                setDrawable();
            }
        });
//        setDrawable();

    }

    //设置删除图片
//    private void setDrawable() {
//        if (length() < 1) {
////            setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
//        } else {
////            setCompoundDrawablesWithIntrinsicBounds(null, null, delete, null);
//        }
//    }

    // 处理删除事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        if (delete != null && event.getAction() == MotionEvent.ACTION_UP) {
//            int eventX = (int) event.getRawX();
//            int eventY = (int) event.getRawY();
//            Rect rect = new Rect();
//            getGlobalVisibleRect(rect);
//            rect.left = rect.right - 150;
//            if (rect.contains(eventX, eventY))
//                setText("");
//        }
        return super.onTouchEvent(event);
    }


    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }
}
